import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/foundation.dart';
import 'package:syria_sports/dialogs/soon_dialog.dart';
import 'package:syria_sports/presentation/photo_view_screen/photo_view_screen_screen.dart';
import 'package:syria_sports/widgets/app_bar/outline_icon_button.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../../data/remote/launcher_url_in_platform.dart';
import '../../exercises/exercises_page/bloc/exercises_screen.dart';
import '../../sports/gym_sports_page/bloc/sports_screen.dart';
import '../../gyms/gyms_page/models/gym.dart';
import '../../exercises/public_exercises_page/bloc/exercises_screen.dart';
import '../public_gyms_page/models/public_gym.dart';
import 'bloc/gym_info_bloc.dart';
import 'package:flutter/material.dart';
import 'package:syria_sports/core/app_export.dart';
import 'package:syria_sports/widgets/app_bar/appbar_subtitle.dart';
import 'package:syria_sports/widgets/app_bar/custom_app_bar.dart';

class GymInfoScreen extends StatelessWidget {
  final Gym gym;
  final bool isPublic;

  const GymInfoScreen({Key? key, required this.gym, this.isPublic = false})
      : super(key: key);

  static Widget builder(BuildContext context, Gym gym, bool isPublic) {
    return BlocProvider<GymInfoBloc>(
        create: (context) =>
            GymInfoBloc(GymInfoState())..add(ProfileInitialEvent()),
        child: GymInfoScreen(
          gym: gym,
          isPublic: isPublic,
        ));
  }

  @override
  Widget build(BuildContext context) {
    mediaQueryData = MediaQuery.of(context);
    return Scaffold(
      appBar: AppBar(
        shadowColor: Colors.transparent,
        toolbarHeight: 0,
      ),
      body: BlocBuilder<GymInfoBloc, GymInfoState>(builder: (context, state) {
        return SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              SizedBox(
              height: mediaQueryData.size.height / 3,
              width: mediaQueryData.size.width,
              child: Stack(
                children: [
                  InkWell(
                    onTap: (){
                      Navigator.push(NavigatorService.navigatorKey.currentContext??context, MaterialPageRoute(builder: (context) => PhotoViewScreenScreen(image: (gym.image == null)
                          ? Image.asset(ImageConstant.imgLogo).image
                          : CachedNetworkImageProvider(gym.image!))));
                    },
                    child: Container(
                      height: mediaQueryData.size.height / 3,
                      width: mediaQueryData.size.width,
                      decoration: BoxDecoration(
                          color: theme.textTheme.titleMedium?.color?.withOpacity(.05),
                          borderRadius: const BorderRadius.only(
                            bottomLeft: Radius.circular(32),
                          ),
                          image: DecorationImage(
                            fit: (gym.image == null) ? null : BoxFit.cover,
                            image: (gym.image == null)
                                ? Image.asset(ImageConstant.imgLogo).image
                                : CachedNetworkImageProvider(gym.image!),
                          ),
                      ),
                    ),
                  ),
                  Align(
                    alignment: Alignment.topRight,
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: OutlineIconButton(
                        imagePath: ImageConstant.imgRightIcon,
                        onTapped: () {
                          Navigator.pop(context);
                        },
                      ),
                    ),
                  ),
                ],
              ),
            ),
              Padding(
              padding: EdgeInsets.only(top: 9.v, bottom: 14.v),
              child: Container(
                padding: const EdgeInsets.all(8),
                width: mediaQueryData.size.width,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(gym.name, style: theme.textTheme.titleSmall),
                    SizedBox(height: 8.v),
                    Text(gym.description ?? "",
                        style: theme.textTheme.bodySmall)
                  ],
                ),
              ),
            ),
              ListTile(
              leading: const Icon(Icons.location_on_outlined,
                  color: Color(0xffbdbab9),
              ),
              title: const Text(
                "العنوان:",
                style: TextStyle(fontSize: 14),
              ),
              trailing:(gym.latitude!=null&&gym.longitude!=null)? IconButton(
                onPressed: ()async{
                  var url = 'https://www.google.com/maps/search/?api=1&query=${gym.latitude},${gym.longitude}';
                  launchInBrowser(Uri.tryParse(url)!);
                },
                icon: const Icon(Icons.map_sharp),
              ):null,
              subtitle: SelectableText(
                gym.address,
                style: const TextStyle(color: Color(0xffbdbab9)),
              ),
              //trailing: Icon(Icons.copy, color: Colors.white54),
            ),
              /*if(kDebugMode)
              Card(
              color: Theme.of(context).colorScheme.secondary,
              child: ListTile(
                onTap: () {
                  if (isPublic) {
                    Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => GymSportsScreen.builder(
                                  context: context,
                                  gym: gym,
                                )));
                  } else {
                    Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => GymSportsScreen.builder(
                                  context: context,
                                  gym: gym,
                                )));
                  }
                },
                leading: Icon(Icons.sports_gymnastics_rounded,
                    color: Colors.orange.withOpacity(.6)),
                title: Text("الرياضات", style: theme.textTheme.titleSmall),
                //subtitle: Text(user.level!,style: CustomTextStyles.bodySmallOnPrimary),
                trailing: CustomImageView(
                  imagePath: ImageConstant.imgArrowLeftBlueGray300,
                  height: 32.adaptSize,
                  width: 32.adaptSize,
                ),
              ),
            ),*/
              Card(
              color: Theme.of(context).colorScheme.secondary,
              child: ListTile(
                onTap: () {
                  if (isPublic) {
                    Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => PublicExercisesScreen(
                                  gym: PublicGym.fromGym(gym),
                                )));
                  } else {
                    Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => ExercisesScreen(
                                  gym: gym,
                                )));
                  }
                },
                leading: Icon(Icons.sports_gymnastics_rounded,
                    color: Colors.orange.withOpacity(.6)),
                title: Text("التمارين", style: theme.textTheme.titleSmall),
                //subtitle: Text(user.level!,style: CustomTextStyles.bodySmallOnPrimary),
                trailing: CustomImageView(
                  imagePath: ImageConstant.imgArrowLeftBlueGray300,
                  height: 32.adaptSize,
                  width: 32.adaptSize,
                ),
              ),
            ),
              Card(
                color: Theme.of(context).colorScheme.secondary,
                child: ListTile(
                  onTap: () {
                    /// here error
                    showDialog(
                        barrierDismissible: true,
                        context: NavigatorService.navigatorKey.currentState!.overlay!.context,
                        builder: (BuildContext context) {
                          return const SoonDialog();
                        });
                  },
                  leading: Icon(Icons.sports_gymnastics_rounded,
                      color: Colors.orange.withOpacity(.6)),
                  title: Text("البرامج التدريبية", style: theme.textTheme.titleSmall),
                  //subtitle: Text(user.level!,style: CustomTextStyles.bodySmallOnPrimary),
                  trailing: CustomImageView(
                    imagePath: ImageConstant.imgArrowLeftBlueGray300,
                    height: 32.adaptSize,
                    width: 32.adaptSize,
                  ),
                ),
              ),
            ],
          ),
        );
      }),
    );
  }

  /// Section Widget
  PreferredSizeWidget _buildAppBar(BuildContext context) {
    return CustomAppBar(
        leading: OutlineIconButton(
          imagePath: ImageConstant.imgRightIcon,
          onTapped: () {
            Navigator.pop(context);
          },
        ),
        title: AppbarSubtitle(
            text: "lbl_profile".tr, margin: EdgeInsets.only(left: 12.h)));
  }
}
